package com.czn.shopdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czn.shopdemo.model.Category;
import com.czn.shopdemo.model.Goods;
import com.czn.shopdemo.model.Result;
import com.czn.shopdemo.model.vo.GoodsVO;
import com.czn.shopdemo.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Zenan Chen
 * @date 2023/4/3 10:52
 */
@RestController
@RequestMapping("/category")
@Api(tags = "商品标签管理")
public class CategoryController {
    private static final Logger log = LoggerFactory.getLogger(CategoryController.class);
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("查询商品标签列表")
    @PostMapping("/getCategoryList")
    public Result getCategoryList() {
        try {
            log.info("------------------------->/getCategoryList 查询商品标签列表开始");
            List<Category> result = categoryService.getCategoryList();
            log.info("------------------------->/getCategoryList 查询商品标签列表结束");
            return Result.ok(result);
        } catch (Exception e) {
            log.error("------------------------->/getCategoryList 查询商品标签列表异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("获取商品标签详情")
    @PostMapping("/getCategoryDetail")
    public Result getCategoryDetail(@RequestParam String uuid) {
        try {
            log.info("接受到参数：uuid:{}", uuid);
            log.info("------------------------->/getCategoryDetail 获取商品标签详情开始");
            Category category = categoryService.getCategoryDetail(uuid);
            log.info("------------------------->/getCategoryDetail 获取商品标签详情结束");
            return Result.ok(category);
        } catch (Exception e) {
            log.error("------------------------->/getCategoryDetail -获取商品标签详情异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("保存商品标签")
    @PostMapping("/saveCategory")
    public Result save(@RequestBody Category category) {
        try {
            log.info("接受到参数：category:{}", JSONObject.toJSONString(category));
            log.info("------------------------->/saveCategorys 保存商品标签开始");
            categoryService.saveCategory(category);
            log.info("------------------------->/saveCategorys 保存商品标签结束");
            return Result.ok("保存商品标签成功！");
        } catch (Exception e) {
            log.error("------------------------->/saveCategorys 保存商品标签异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }

    @ApiOperation("删除商品标签")
    @PostMapping("/deleteCategory")
    public Result deleteCategory(@RequestParam String uuid) {
        try {
            log.info("接受到参数：uuid:{}", uuid);
            log.info("------------------------->/saveCategorys 删除商品标签开始");
            categoryService.deleteCategory(uuid);
            log.info("------------------------->/saveCategorys 删除商品标签结束");
            return Result.ok("删除商品标签成功！");
        } catch (Exception e) {
            log.error("------------------------->/saveCategorys 删除商品标签异常【{}】", e.getMessage());
            return Result.build(201, e.getMessage());
        }
    }
}
